﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace InstallManager.Msi
{
    public abstract class MsiCommonDataMessage
    {
        public string Field1 { get; internal set; }
        public string Field2 { get; internal set; }
        public string Field3 { get; internal set; }




        public static MsiCommonDataMessage Parse(string message)
        {
            string field1 = string.Empty;
            string field2 = string.Empty;
            string field3 = string.Empty;
            if (!string.IsNullOrEmpty(message))
            {
                //Regex regex = new Regex("1: (?<field1>[\\d]+) 2: (?<field2>[\\d]+) 3: (?<field3>[\\d]+) 4: (?<field4>[\\d]+)");
                Regex regex = new Regex("1: (?<field1>[\\d]+) ");
                if (regex.IsMatch(message))
                {
                    Match match = regex.Match(message);
                    field1 = match.Groups["field1"].Value;
                }
                regex = new Regex("2: (?<field2>[\\w]+) ");
                if (regex.IsMatch(message))
                {
                    Match match = regex.Match(message);
                    field2 = match.Groups["field2"].Value;
                }
                regex = new Regex("3: (?<field3>[\\w]+) ");
                if (regex.IsMatch(message))
                {
                    Match match = regex.Match(message);
                    field3 = match.Groups["field3"].Value;
                }
                regex = new Regex("Message type: (?<field1>[\\d]+)");
                if (regex.IsMatch(message))
                {
                    Match match = regex.Match(message);
                    field1 = match.Groups["field1"].Value;
                    regex = new Regex("Argument: (?<fields>[\\w\\d\\s,]+)");
                    if (regex.IsMatch(message))
                    {
                        match = regex.Match(message);
                        string[] fields = match.Groups["fields"].Value.Split(',');
                        if (fields.Count() >= 1)
                        {
                            field2 = fields[0].Trim();
                        }
                        if (fields.Count() >= 2)
                        {
                            field3 = fields[1].Trim();
                        }
                    }

                }

            }
            if (!string.IsNullOrWhiteSpace(field1))
            {
                MsiCommonDataMessage msg = null;
                switch (field1)
                {
                    case "0":
                        msg = new MsiLanguageMessage();
                        break;
                    case "1":
                        msg = new MsiCaptionMessage();
                        break;
                    case "2":
                        msg = new MsiCancelShowMessage();
                        break;
                    default:
                        break;
                }
                if (msg != null)
                {
                    msg.Field1 = field1;
                    msg.Field2 = field2;
                    msg.Field3 = field3;
                    return msg;
                }
            }
            return null;
        }

    }

    public class MsiLanguageMessage : MsiCommonDataMessage
    {
        public string LanguageId
        {
            get
            {
                return Field2;
            }
        }
        public string ANSICodePage
        {
            get
            {
                return Field3;
            }
        }
        public override string ToString()
        {
            return String.Format("LanguageId:{0} ANSICodePage:{1}", LanguageId, ANSICodePage);
        }
    }
    public class MsiCaptionMessage : MsiCommonDataMessage
    {
        public string Caption
        {
            get
            {
                return Field2;
            }
        }
        public override string ToString()
        {
            return String.Format("Caption:{0}", Caption);
        }
    }
    public class MsiCancelShowMessage : MsiCommonDataMessage
    {
        public bool? ShowCancelButton
        {
            get
            {
                switch (Field2)
                {
                    case "0":
                        return false;
                    case "1":
                        return true;
                    default:
                        return null;
                }

            }
        }
        public override string ToString()
        {
            return String.Format("ShowCancelButton:{0}", ShowCancelButton);
        }
    }
}
